//
// Created by wk on 2024/1/2.
//

#include "EncryptTypeXG.h"


EncryptTypeXG::EncryptTypeXG() {
    mEncryptType = EncryptType::EncryptType_XG;
}

EncryptTypeXG::~EncryptTypeXG() {

}

void EncryptTypeXG::xingguang_trans_header(struct XG_HEAD *xst) {
    DWORD *p = (DWORD *) xst;

    DWORD m = xst->key;
    for (int i = 0; i < sizeof(struct XG_HEAD) / sizeof(DWORD) - 1; ++i) {
        p[i] ^= m;
    }
}

bool EncryptTypeXG::isEncryptType(FILE *inputFile) {
    fseek(inputFile, 0, SEEK_SET);

    memset(&xgHead, 0, sizeof(xgHead));

    int ret = fread(&xgHead, 1, sizeof(xgHead), inputFile);
    if (ret != sizeof(xgHead)) {
        return false;
    }

    xingguang_trans_header(&xgHead);

    const static BYTE XGDX[4] = {'X', 'G', 'D', 'X'};

    if (xgHead.nFlags[0] == XGDX[0] &&
        xgHead.nFlags[1] == XGDX[1] &&
        xgHead.nFlags[2] == XGDX[2] &&
        xgHead.nFlags[3] == XGDX[3]) {
        return true;
    }
    return false;
}
